package chap8;
//***********************************************************************
//  PP8_6.java              Author:GK
//
//  管理不超过30个账户。
//***********************************************************************

public class AccountCollection {
    private Account[] accounts;
    private int count;

    // 构造数组。
    public AccountCollection()
    {
        accounts = new Account[30];
        count = 0;
    }

    //  在数组内添加账户。
    public void addAccount(String name, long acctNumber, double balance)
    {
        if ( count < accounts.length )
        {
            accounts[count] = new Account(name, acctNumber, balance);
            count++;
        }
    }

    //  在数组内存款。
    public void saveMoney(String name, long acctNumber, double balance)
    {
        for(int a = 0; a < 30; a++)
            if(accounts[a] != null) {
                if (accounts[a].getName().equals(name) && accounts[a].getNumber() == acctNumber)
                    accounts[a].deposit(balance);
            }
    }

    //  在数组内取款。
    public void drawMoney(String name, long acctNumber, double balance)
    {
        for(int a =0; a < 30; a++)
            if(accounts[a] != null) {
                if (accounts[a].getName().equals(name) && accounts[a].getNumber() == acctNumber) {
                    if (accounts[a].getBalance() > balance)
                        accounts[a].withdraw(balance, 1);
                }
            }
    }

    //  利息。
    public void addInterest()
    {
        for (Account account : accounts) {
            if (account != null) {
                account.addInterest();
            }
        }
    }

    public String toString()
    {
        String result = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

        for (int a = 0; a < 30; a++)
        {
            if (accounts[a] != null)
            result += accounts[a].toString() + "\n\n";
        }
        return result;
    }
}
